package natappvip.liujinliang.dao;

import java.sql.Connection;
import java.sql.ResultSet;

import natappvip.liujinliang.db.ConnectionManager;
import natappvip.liujinliang.db.DataManipulationLang;
import natappvip.liujinliang.db.SqlLang;
import natappvip.liujinliang.model.FileEle;
import natappvip.liujinliang.util.FileEleTrans;

/**
 * @ClassName:  FileOperationDAOImpl   
 * @Despt: 文件操作dao层  
 * @Auth: Liujinliang 
 * @Date: 2018年7月29日 下午9:40:00
 */
public class FileOperationDAOImpl implements FileOperationDAO {

	@Override
	public int add(FileEle ele) {
		String sql = SqlLang.sqlAdd;
		Connection conn = ConnectionManager.getConnection();
		try {
			DataManipulationLang.update(conn, sql, ele);
			ConnectionManager.commitTransaction();
			return 0;
		} catch (Exception e) {
			ConnectionManager.rollbackTransaction();
		} finally {
			ConnectionManager.closeConnection();
		}
		return -1;
	}

	@Override
	public int del(String uuid, FileEle ele) {
		String sql = SqlLang.sqlDel;
		Connection conn = ConnectionManager.getConnection();
		try {
			DataManipulationLang.update(conn, sql, ele);
			ConnectionManager.commitTransaction();
			return 0;
		} catch (Exception e) {
			ConnectionManager.rollbackTransaction();
		} finally {
			ConnectionManager.closeConnection();
		}
		return -1;
	}

	@Override
	public FileEle get(String uuid) {
		FileEle ele = new FileEle();
		String sql = SqlLang.sqlGet;
		Connection conn = ConnectionManager.getConnection();
		try {
			ResultSet res = DataManipulationLang.query(conn, sql, uuid);
			FileEleTrans.ReSettoFileEle(res, ele);
			ConnectionManager.commitTransaction();
			return ele;
		} catch (Exception e) {
			ConnectionManager.rollbackTransaction();
		} finally {
			ConnectionManager.closeConnection();
		}
		return null;
	}

}
